package com.pioneer.admin.controller.book;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pioneer.common.annotation.Log;
import com.pioneer.common.core.controller.BaseController;
import com.pioneer.common.core.domain.AjaxResult;
import com.pioneer.common.enums.BusinessType;
import com.pioneer.web.book.domain.Book;
import com.pioneer.web.book.service.IBookService;
import jakarta.annotation.Resource;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * 图书
 *
 * @author xx
 */
@RestController
@RequestMapping("/library/book")
public class BookController extends BaseController {

    @Resource
    private IBookService bookService;

    /**
     * 获取图书列表
     *
     * @param book 查询条件
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:list')")
    @GetMapping("/list")
    public AjaxResult list(Book book) {
        boolean isPage = startPage();
        List<Book> list = bookService.list(Wrappers.query(book));
        return success(isPage, list);
    }

    /**
     * 根据图书id获取详细信息
     *
     * @param bookId 图书id
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:query')")
    @GetMapping(value = "/{bookId}")
    public AjaxResult getInfo(@PathVariable Long bookId) {
        return AjaxResult.success(bookService.getById(bookId));
    }

    /**
     * 新增图书
     *
     * @param book 图书
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:add')")
    @Log(title = "图书", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody Book book) {
        return toAjax(bookService.addBook(book));
    }

    /**
     * 修改图书
     *
     * @param book 图书
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:edit')")
    @Log(title = "图书", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody Book book) {
        return toAjax(bookService.updateBook(book));
    }

    /**
     * 删除图书
     *
     * @param bookIds 图书ids组
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:remove')")
    @Log(title = "图书", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bookIds}")
    public AjaxResult remove(@PathVariable Long[] bookIds) {
        return toAjax(bookService.deleteBook(Arrays.asList(bookIds)));
    }

    /**
     * 借阅图书
     *
     * @param bookIds 书籍ID列表
     * @return 结果
     */
    @PreAuthorize("@ss.hasPerm('library:book:borrow')")
    @Log(title = "图书", businessType = BusinessType.OTHER)
    @PostMapping("/borrow/{bookIds}")
    public AjaxResult borrow(@PathVariable Long[] bookIds) {
        return toAjax(bookService.borrowBook(Arrays.asList(bookIds)));
    }

}
